/*
 * @Author: lyx 2858535420@qq.com
 * @Date: 2023-02-24 14:55:09
 * @LastEditors: lyx 2858535420@qq.com
 * @LastEditTime: 2023-04-27 10:51:00
 * @FilePath: /antd-vue2-template/src/filters/index.js
 * @Description: 通用的 filter
 */

import frmMoment from 'moment';

/**
 * Number formatting
 * like 10000 => 10k
 * @param {number} num
 * @param {number} digits
 */
export function numberFormatter(num, digits) {
  const si = [
    { value: 1E18, symbol: 'E' },
    { value: 1E15, symbol: 'P' },
    { value: 1E12, symbol: 'T' },
    { value: 1E9, symbol: 'G' },
    { value: 1E6, symbol: 'M' },
    { value: 1E3, symbol: 'k' }
  ];
  for (let i = 0; i < si.length; i++) {
    if (num >= si[i].value) {
      return (num / si[i].value).toFixed(digits).replace(/\.0+$|(\.[0-9]*[1-9])0+$/, '$1') + si[i].symbol;
    }
  }
  return num.toString();
}

/**
 * 10000 => "10,000"
 * @param {number} num
 */
export function toThousandFilter(num) {
  return (+num || 0).toString().replace(/^-?\d+/g, m => m.replace(/(?=(?!\b)(\d{3})+$)/g, ','));
}

/**
 * Upper case first char
 * @param {String} string
 */
export function uppercaseFirst(string) {
  return string.charAt(0).toUpperCase() + string.slice(1);
}

/**
 * 格式化时间
 * @param {*} dataStr 
 * @param {*} pattern 
 * @returns 
 */
export function moment(dataStr, pattern = 'YYYY-MM-DD HH:mm:ss') {
  return frmMoment(dataStr).format(pattern);
}

/**
 * 格式化时间
 * @param {*} dataStr 
 * @param {*} pattern 
 * @returns 
 */
export function dayjs(dataStr, pattern = 'YYYY-MM-DD HH:mm:ss') {
  return frmMoment(dataStr).format(pattern);
}
